/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import Biz.StockBO;
import Entity.Stock;
import java.sql.SQLException;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Nghia
 */
public class Model_Stock extends AbstractTableModel {

    private static final int ITEMCODE_INDEX = 0;
    private static final int INAME_INDEX = 1;
    private static final int ICOST_INDEX = 2;
    private static final int INUM_INDEX = 3;
    private static final int DNUM_INDEX = 4;
    protected String[] columnNames;
    protected java.util.List<Stock> stockList;

    public Model_Stock() {
    }

    public Model_Stock(String[] columnNames, java.util.List<Stock> stockList) {
        this.columnNames = columnNames;
        this.stockList = stockList;
    }

    @Override
    public String getColumnName(int i) {
        return columnNames[i];
    }

    @Override
    public Class getColumnClass(int column) {
        switch (column) {
            case ITEMCODE_INDEX:
                return Integer.class;
            case INAME_INDEX:
                return String.class;
            case ICOST_INDEX:
                return Integer.class;
            case INUM_INDEX:
                return Integer.class;
            case DNUM_INDEX:
                return Integer.class;
            default:
                return Object.class;
        }
    }

    @Override
    public int getRowCount() {
        return stockList.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int row, int column) {
        Stock d = (Stock) stockList.get(row);
        switch (column) {
            case ITEMCODE_INDEX:
                return d.getItemCode();
            case INAME_INDEX:
                return d.getIName();
            case ICOST_INDEX:
                return d.getICost();
            case INUM_INDEX:
                return d.getINum();
            case DNUM_INDEX:
                return d.getDNum();
            default:
                return new Object();
        }
    }

    @Override
    public void setValueAt(Object value, int row, int column) {
        Stock t = (Stock) stockList.get(row);
        switch (column) {
            case 0:
                t.setItemCode((Integer) value);
            case 1:
                t.setIName(String.valueOf(value));
            case 2:
                t.setICost((Integer) value);
            case 3:
                t.setINum((Integer) value);
            case 4:
                t.setDNum((Integer) value);
            default:
                System.out.println("Invalid index");
        }
        fireTableCellUpdated(row, column);
    }
    
    public void add(String IName, int ICost, int INum, int DNum) throws SQLException {
        StockBO db = new StockBO();
        db.addStock(IName, ICost, INum, DNum);
    }

    public void edit(int ItemCode, String IName, int ICost, int INum, int DNum) throws SQLException {
        StockBO db = new StockBO();
        db.editStock(ItemCode, IName, ICost, INum, DNum);
    }

    public void delete(int StockID) throws SQLException {
        StockBO db = new StockBO();
        db.deleteStock(StockID);
    }
}
